package com.cls.system.service.impl;

import com.cls.common.entity.QueryRequest;
import com.cls.system.entity.Configure;
import com.cls.system.entity.ConfigureQualification;
import com.cls.system.mapper.ConfigureMapper;
import com.cls.system.mapper.ConfigureQualificationMapper;
import com.cls.system.service.IConfigureService;
import com.cls.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

/**
 * 配置表 Service实现
 *
 * @author slx
 * @date 2020-11-17 11:51:11
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ConfigureServiceImpl extends ServiceImpl<ConfigureMapper, Configure> implements IConfigureService {

    @Autowired
    private final ConfigureQualificationMapper configureQualificationMapper;

    @Override
    public PageInfo<Configure> findConfigures(QueryRequest request, Configure configure) {
        LambdaQueryWrapper<Configure> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Configure> list= this.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<Configure> findConfigures(Configure configure) {
	    LambdaQueryWrapper<Configure> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
        queryWrapper.eq(Configure::getSnapshot,false);
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createConfigure(Configure configure) {
        this.save(configure);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateConfigure(Configure configure) {
        this.saveOrUpdate(configure);
    }


    /**
    * 删除
    * @param ids
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteConfigures(String[] ids) {
        List< String > list = Arrays.asList(ids);
        this.removeByIds(list);
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long snapshotConfigure(Configure configure) {
        LambdaQueryWrapper<Configure> queryWrapper = new LambdaQueryWrapper<>();
        if(configure.getSnapshot() != null){
            queryWrapper.eq(Configure::getSnapshot, configure.getSnapshot());
        }

        if(configure.getConfigureId() != null){
            queryWrapper.eq(Configure::getConfigureId, configure.getConfigureId());
        }

        // 获取最新标准快照
        List<Configure> snapshots = baseMapper.selectList(queryWrapper);
        if(snapshots == null || snapshots.size() != 1){
            return -1L;
        }
        Configure snapshot = snapshots.get(0);
        // 获取最新标准下各项指标快照
        List<ConfigureQualification> snapshotConfigureQualifications = configureQualificationMapper.findByConfigureId(snapshot.getConfigureId());

        // 插入标准快照，并生成新的标准ID
        snapshot.setSnapshot(true);
        save(snapshot);

        for(ConfigureQualification one:snapshotConfigureQualifications){
            // 将新的标准ID和即将插入的指标数据绑定，并插入指标数据
            one.setConfigureId(snapshot.getConfigureId());
            configureQualificationMapper.insert(one);
        }
        return snapshot.getConfigureId();
    }

}
